package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Permission;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import io.netty.util.internal.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping(path="/menu")
public class MenuController {
    @Reference
    private MenuService menuService;


    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @GetMapping(path = "/page")
    public Result findPage(QueryPageBean queryPageBean ){
        //分页查询菜单
        log.info("进入分页查询方法");
        try {
            PageResult pageResult = menuService.pageQuery(queryPageBean);
            return  new Result(true,MessageConstant.QUERY_MENU_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }

    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public Result findAll(){
        log.info("进入查询子父菜单");
        //查询子父菜单
        try {
            List<Menu> menuList = menuService.findParentChild();
            return  new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menuList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }

    }


    /**
     * 新增菜单
     */
    @PostMapping
    public Result handleAdd (@RequestBody Menu menu, Integer[] permissionIds){
        log.info("进入添加方法");
        try {
            menuService.handleAdd(menu,permissionIds);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 删除菜单
     */
    @DeleteMapping(path = "/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        try {
            log.info("进入删除方法");
            menuService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }



    /**
     * 编辑菜单根据id进行回显
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result handleUpdate(@PathVariable("id") Integer id){
        log.info("进入了编辑回显菜单方法");
        try {
            Menu menu =  this.menuService.selectMenuById(id);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }

    /**
     * 确认修改
     * @param menu
     * @param permissionIds
     * @return
     */
    @PutMapping
    public Result handleEdit(@RequestBody Menu menu,Integer[] permissionIds){
        try {
            log.info("进入确认修改方法");
            //调用Service
            menuService.update(menu,permissionIds);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }

    @GetMapping(path = "/{id}/permissions")
    public Result findPermission(@PathVariable("id") Integer id){
        log.info("进入根据id查询相关permission");
        try {
            //调用Service
            Integer[] permissionids = menuService.selectPermissionById(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissionids);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }

    }

}
